﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using YunShop_WebAPI.BLL.SxfPay;
using YunShop_WebAPI.BLL;
using YunShop_WebAPI.Model;
using Newtonsoft.Json.Linq;
using System.Net.Sockets;
using System.Web.Services.Description;
using YunShop_WebAPI.BLL.WxThirdParty;
using System.Web.Http.Results;

namespace YunShop_WebAPI.API.Controllers
{
    public class MallController : ApiController
    {

        /// <summary>  
        /// 接收回调  
        /// </summary>  
        [Route("{id}/{action}")]
        [ActionName("CallBack")]
        [HttpPost, HttpGet]
        public object CallBack(string id, [FromBody] Hashtable ht)
        {
            string AppId = ht["AppId"] as string;
            long CreateTime = CommonFun.ConvertToBigInt(ht["CreateTime"]);
            string InfoType = ht["InfoType"] as string;
            string ComponentVerifyTicket = ht["ComponentVerifyTicket"] as string;

            return "success";

        }



        /// <summary>
        ///  授权事件接收回调
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("WXVerityCallBack")]
        public Result_Model WXVerityCallBack()
        {
            return new Result_Model(1, "WXVerityCallBack获取成功");
        }





        /// <summary>
        /// 1.15、商品详情
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetGoodsDetails")]
        public Result_Model GetGoodsDetails()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            Int64 goods_id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["goods_id"]);
            Hashtable data = MallCommon.GetGoodsDetail(shop_appid, goods_id);
            return new Result_Model(1, "获取成功", data);
        }





        /// <summary>
        /// 小程序用户登录
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("OnUserLogin")]
        public Result_Model OnUserLogin()
        {
            string appid = CommonFun.ConvertToString(HttpContext.Current.Request["appid"]);
            string code = CommonFun.ConvertToString(HttpContext.Current.Request["code"]);
            int CorpId = CommonFun.ConvertToInt(HttpContext.Current.Request["CorpId"]);
            long FStockID = CommonFun.ConvertToBigInt(HttpContext.Current.Request["FStockID"]);
            int FParentID = CommonFun.ConvertToInt(HttpContext.Current.Request["FParentID"]);
            int status = 0;

            string message = "获取成功";
            ArrayList data = MallCommon.OnUserLogin(appid, code, ref status, ref message, CorpId, FStockID, FParentID);
            if (data == null || data.Count == 0)
            {
                status = 0;
            }
            return new Result_Model(status, message, data);
        }

        /// <summary>
        /// 手机号一键登录
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("LoginByPhoneOneKey")]
        public async Task<Result_Model> LoginByPhoneOneKey()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long m_CorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            long m_PtCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.TopCorpID); // 3
            long m_StockID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.StockID); // 3
            string m_code = CommonFun.ConvertToString(HttpContext.Current.Request["code"]);
            string m_unionid = CommonFun.ConvertToString(HttpContext.Current.Request["unionId"]);
            string m_appId = CommonFun.ConvertToString(HttpContext.Current.Request["appId"]);
            int CorpId = CommonFun.ConvertToInt(HttpContext.Current.Request["CorpId"]);
            if (m_CorpID == 0)
            {
                m_CorpID = CorpId;
            }

            long FStockID = CommonFun.ConvertToBigInt(HttpContext.Current.Request["FStockID"]);
            if (m_StockID == 0)
            {
                m_StockID = FStockID;
            }
            int FParentID = CommonFun.ConvertToInt(HttpContext.Current.Request["FParentID"]);
            return await MallService.LoginByPhoneOneKey((int)m_CorpID, m_code, m_unionid, m_StockID, m_appId, FParentID);
        }


        [HttpPost, HttpGet]
        [ActionName("GetAddressList")]
        public async Task<Result_Model> GetAddressList()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long m_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            if (m_WeixinID > 0)
            {
                ArrayList dt = MallService.GetAddressList(m_WeixinID);
                status = 1;
                return new Result_Model(status, "获取成功！", dt);
            }
            return new Result_Model(status, "WeixinID 为空,请重新登录");

        }


        [HttpPost, HttpGet]
        [ActionName("GetAddressById")]
        public async Task<Result_Model> GetAddressById()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["id"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            ArrayList dt = MallService.GetAddressById(id);
            status = 1;

            return new Result_Model(status, "获取成功！", dt[0]);
        }

        [HttpPost, HttpGet]
        [ActionName("GetPay")]
        public Result_Model GetPay()
        {
            int status = 0;
            ArrayList dt = MallService.GetPay();
            status = 1;
            return new Result_Model(status, "获取成功！", dt);
        }


        [HttpPost, HttpGet]
        [ActionName("DelAddress")]
        public Result_Model DelAddress()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["id"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            bool res = MallService.DelAddress(id);
            status = 1;

            return new Result_Model(status, res ? "删除成功！" : "删除失败！", res);
        }

        [HttpPost, HttpGet]
        [ActionName("SaveAddress")]
        public Result_Model SaveAddress()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            long m_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 1

            bool res = MallService.SaveAddress(json, m_WeixinID);
            status = 1;

            return new Result_Model(status, "编辑成功！", res);
        }


        [HttpPost, HttpGet]
        [ActionName("SetDefaultAddress")]
        public Result_Model SetDefaultAddress()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["id"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            long m_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 1
            if (m_WeixinID == 0 || id == 0)
            {
                return new Result_Model(status, "设置失败,id为空或用户为登录!");

            }

            bool res = MallService.SetDefaultAddress(id, m_WeixinID);
            return new Result_Model(res ? 1 : 0, res ? "设置成功！" : "设置失败!", res);
        }



        [HttpPost, HttpGet]
        [ActionName("GetHomeData")]
        public Result_Model GetHomeData()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            int mCorpID = (int)SystemInfo.GetSystemAppID(shop_appid, idType: SystemAppID.CorpID); // 1
            int mPtCorpID = (int)SystemInfo.GetSystemAppID(shop_appid, idType: SystemAppID.TopCorpID); // 3

            int status = 0;
            string message = "获取成功";
            ArrayList AdvCar = MallService.GetAdvList(DataType: 0, mCorpID, 4);  //广告轮播
            ArrayList AdvPosit = MallService.GetAdvList(DataType: 2, mCorpID, 4);  //广告位广告
            ArrayList ClassNav = MallService.GetAdvList(DataType: 1, mCorpID, 4);  //分类导航
            Dictionary<string, object> jsonData = new Dictionary<string, object>() {
                {
                "AdvCar",AdvCar
                },
                {
                   "AdvPosit",AdvPosit
                },
                {
                   "ClassNav",ClassNav
                }
            };
            status = 1;

            return new Result_Model(status, message, jsonData);
        }

        [HttpGet]
        [HttpPost]
        [ActionName("GetGoodsList")]
        public Result_Model GetGoodsList()
        {

            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string appid = CommonFun.ConvertToString(HttpContext.Current.Request["appid"]);
            int mCorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int mPageIndex = CommonFun.ConvertToInt(HttpContext.Current.Request["page_index"]) - 1;
            int mPageSize = CommonFun.ConvertToInt(HttpContext.Current.Request["page_size"]);
            int mClassID = CommonFun.ConvertToInt(HttpContext.Current.Request["class_id"]);
            int mBrandID = CommonFun.ConvertToInt(HttpContext.Current.Request["brand_id"]);
            int mOrderType = CommonFun.ConvertToInt(HttpContext.Current.Request["order_type"]);
            string mConditon = CommonFun.ConvertToString(HttpContext.Current.Request["condition"]);

            int status = 0;

            string message = "获取成功";
            status = 1;
            //pr_wap_GetGoodFormClass
            ArrayList jsonData = MallService.GetGoodsList(mCorpID, mPageIndex, mPageSize, mClassID, mBrandID, mConditon, mOrderType);
            int count = MallService.GetGoodsListCount(mCorpID, mClassID, mBrandID, mConditon, mOrderType); ;
            return new Result_Model(status, message, jsonData, count);
        }

        #region 计算运费
        /// <summary>
        /// 计算运费
        /// </summary>
        /// <param name="FCmpID"></param>
        /// <param name="Pid"></param>
        /// <param name="Cid"></param>
        /// <param name="ArID"></param>
        /// <param name="Weight"></param>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        [ActionName("AccExpMoney")]
        public Result_Model AccExpMoney()
        {
            Hashtable ht = new Hashtable();
            ht["status"] = 0;
            ht["msg"] = "计算运费失败！";
            ht["ExpMoney"] = "0";

            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string strJson = CommonFun.ConvertToString(HttpContext.Current.Request["strJson"]);
            int mCorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            long F_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 1
            try
            {
                decimal li_OrderExpMoney = 0;
                long corpId = 0;
                int fqty = 0;
                decimal weight = 0;
                decimal bulk = 0;
                T_AccExpMoney _model = JsonConvert.DeserializeObject<T_AccExpMoney>(strJson);
                corpId = _model.ImportFlag == 1 ? 1 : mCorpID;
                DataTable dtCart = ShopCartBill.GetShopCart(F_WeixinID, corpId, _model.ImportFlag);
                DataTable dt = ShopCartBill.AccExpMoney(_model.FCmpID, _model.Pid, _model.Cid, _model.ArID, corpId);
                if (ShopCartBill.IsPinkage(corpId))
                {
                    foreach (DataRow dr in dtCart.Rows)
                    {
                        li_OrderExpMoney += string.IsNullOrWhiteSpace(dr["RealMoney"].ToString()) ? 0 : Convert.ToDecimal(dr["RealMoney"].ToString());
                    }
                    li_OrderExpMoney = li_OrderExpMoney >= Convert.ToDecimal(dt.Rows[0]["FMin"].ToString()) ? 0 : Convert.ToDecimal(dt.Rows[0]["FMax"].ToString());
                    ht["status"] = 1;
                    ht["msg"] = "计算运费成功！";
                    ht["ExpMoney"] = li_OrderExpMoney;
                    return new Result_Model((int)ht["status"], (string)ht["msg"], ht["ExpMoney"]);
                }
                if (dtCart != null && dtCart.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtCart.Rows)
                    {
                        fqty += string.IsNullOrWhiteSpace(dr["FQty"].ToString()) ? 0 : Convert.ToInt32(dr["FQty"].ToString());
                        weight += string.IsNullOrWhiteSpace(dr["FWeight"].ToString()) ? 0 : Convert.ToDecimal(dr["FWeight"].ToString());
                        bulk += string.IsNullOrWhiteSpace(dr["FBulk"].ToString()) ? 0 : Convert.ToDecimal(dr["FBulk"].ToString());
                    }
                }
                if (dt != null && dt.Rows.Count > 0)
                {
                    decimal baseQty = Convert.ToDecimal(dt.Rows[0]["FBaseQty"].ToString());
                    decimal freight = Convert.ToDecimal(dt.Rows[0]["FFreight"].ToString());
                    decimal othQty = Convert.ToDecimal(dt.Rows[0]["FOthQty"].ToString());
                    decimal othMoney = Convert.ToDecimal(dt.Rows[0]["FOthMoney"].ToString());
                    decimal calculateType = Convert.ToDecimal(dt.Rows[0]["FCalculateType"].ToString());

                    if (calculateType == 0)
                    {
                        if (othQty == 0)
                        {
                            othQty = 1;
                            decimal m = Math.Ceiling((fqty - baseQty) / othQty);
                            if (m < 0)
                            {
                                m = 0;
                            }
                            li_OrderExpMoney = freight + m * othMoney;
                        }
                        else
                        {
                            li_OrderExpMoney = freight;
                        }
                    }
                    else if (calculateType == 1)
                    {
                        if (othQty == 0) othQty = 1;
                        decimal m = Math.Ceiling((fqty * weight - baseQty * weight) / othQty);
                        if (m < 0)
                        {
                            m = 0;
                        }
                        li_OrderExpMoney = freight + m * othMoney;
                    }
                    else if (calculateType == 2)
                    {
                        if (othQty == 0) othQty = 1;
                        decimal m = Math.Ceiling((fqty * bulk - baseQty * bulk) / othQty);
                        if (m < 0)
                        {
                            m = 0;
                        }
                        li_OrderExpMoney = freight + m * othMoney;
                    }
                    ht["status"] = 1;
                    ht["msg"] = "计算运费成功！";
                    ht["ExpMoney"] = li_OrderExpMoney;
                }
                else
                {
                    ht["status"] = 1;
                    ht["msg"] = "计算运费成功！";
                    ht["ExpMoney"] = 0;
                    //ht["msg"] = "未设置运费！";
                }
                return new Result_Model((int)ht["status"], (string)ht["msg"], ht["ExpMoney"]);
            }

            catch (Exception ex)
            {
                //Console.WriteLine("AccExpMoney出错:", ex.Message);
                Console.WriteLine(ex.Message);

            }

            return new Result_Model((int)ht["status"], (string)ht["msg"], ht["ExpMoney"]);
        }
        #endregion


        [HttpPost, HttpGet]
        [ActionName("GetShopCart")]
        public Result_Model GetShopCart()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            ArrayList dt = DBHelper.DataTable2ArrayList(ShopCartBill.GetShopCart(m_WeixinId, m_CorpID, 0));
            status = 1;

            return new Result_Model(status, "编辑成功！", dt);
        }


        [HttpPost, HttpGet]
        [ActionName("AddCart")]
        public Result_Model AddCart()
        {
            int status = 0;
            Int64 goods_id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["goods_id"]);
            decimal qty = CommonFun.ConvertToBigInt(HttpContext.Current.Request["qty"]);
            decimal price = CommonFun.ConvertToBigInt(HttpContext.Current.Request["price"]);
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            bool res = ShopCartBill.AddCart(goods_id, qty, price, m_CorpID, m_WeixinId, 0);
            status = 1;

            return new Result_Model(status, "编辑成功！", res);
        }

        [HttpPost, HttpGet]
        [ActionName("DelCartGoods")]
        public Result_Model DelCartGoods()
        {
            int status = 0;
            long m_FEntryID = CommonFun.ConvertToBigInt(HttpContext.Current.Request["FEntryID"]);
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            bool res = ShopCartBill.DelCartGoods(FEntryID: m_FEntryID);
            status = 1;

            return new Result_Model(status, "编辑成功！", res);
        }

        [HttpPost, HttpGet]
        [ActionName("UpCart")]
        public Result_Model UpCart()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            Int64 goods_id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["goods_id"]);
            decimal qty = CommonFun.ConvertToBigInt(HttpContext.Current.Request["qty"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            bool res = ShopCartBill.UpCart(goods_id, qty, m_WeixinId, m_CorpID);
            status = 1;

            return new Result_Model(status, "编辑成功！", res);
        }

        [HttpPost, HttpGet]
        [ActionName("GetMallPayment")]
        public Result_Model GetMallPayment()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long KInterID = CommonFun.ConvertToBigInt(HttpContext.Current.Request["KInterID"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            DataTable dataTable = ShopCartBill.Get_Mall_Payment(KInterID: KInterID);
            ArrayList arrayList = DBHelper.DataTable2ArrayList(dataTable);
            status = arrayList.Count > 0 ? 1 : 0;
            string msg = arrayList.Count > 0 ? "获取成功！" : "无此数据！";
            return new Result_Model(status, msg, arrayList);
        }


        #region 提交订单
        /// <summary>
        /// 提交订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        [ActionName("OrderSubmit")]
        public Result_Model OrderSubmit()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3

            try
            {
                y_Mall_Shopping _model = new y_Mall_Shopping();
                Hashtable ht = new Hashtable();
                ht["status"] = 0;
                ht["msg"] = "提交订单失败";
                if (m_UserID == 0)
                {
                    ht["status"] = 0;
                    ht["msg"] = "请先登录，才允许提交订单！";
                    return new Result_Model((int)ht["status"], (string)ht["msg"], ht);
                }
                long li_FWeixinID = m_WeixinId;
                _model.FPrvID = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_Pid"]) ? 0 : Convert.ToInt32(HttpContext.Current.Request["J_Pid"]);
                _model.FCityID = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_Cid"]) ? 0 : Convert.ToInt32(HttpContext.Current.Request["J_Cid"]);
                _model.FAreaID = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_Arid"]) ? 0 : Convert.ToInt32(HttpContext.Current.Request["J_Arid"]);
                _model.FShippingID = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_CmpID"]) ? 0 : Convert.ToInt32(HttpContext.Current.Request["J_CmpID"]);
                _model.FPayID = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_PayID"]) ? 0 : Convert.ToInt32(HttpContext.Current.Request["J_PayID"]);
                _model.FCouponID = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_FCouponID"]) ? 0 : Convert.ToInt32(HttpContext.Current.Request["J_FCouponID"]);
                _model.FCouponMoney = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_FCouponMoney"]) ? 0 : Convert.ToDecimal(HttpContext.Current.Request["J_FCouponMoney"]);
                _model.FShippingAmt = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_ExpMoney"]) ? 0 : Convert.ToDecimal(HttpContext.Current.Request["J_ExpMoney"]);
                _model.FImportsFlag = string.IsNullOrWhiteSpace(HttpContext.Current.Request["J_FImportFlag"]) ? -1 : Convert.ToInt32(HttpContext.Current.Request["J_FImportFlag"]);
                _model.FBillFlag = HttpContext.Current.Request["J_FBillFlag"] == "on" ? 1 : 0;
                _model.FConsignee = HttpContext.Current.Request["J_FConsignee"];
                _model.FAddress = HttpContext.Current.Request["J_FPrCityArea"] + HttpContext.Current.Request["J_FAddress"];
                _model.FTelephone = HttpContext.Current.Request["J_FPhone"];
                _model.FMessage = HttpContext.Current.Request["J_FMessage"];
                _model.type = HttpContext.Current.Request["id"];
                string goodsSeckillId = HttpContext.Current.Request["FSckillDetailID"];

                long li_CorpID = m_CorpID;
                if (_model.type == "1" && !string.IsNullOrEmpty(goodsSeckillId))
                {

                }
                else
                {
                    DataTable dt = ShopCartBill.GetShopCart(li_FWeixinID, li_CorpID, _model.FImportsFlag);
                    if (dt.Rows.Count <= 0)
                    {
                        ht["msg"] = "购物车商品无效，请重新下单！";
                        return new Result_Model((int)ht["status"], (string)ht["msg"], ht);
                    }
                }

                if (_model.FPrvID <= 0 || _model.FCityID <= 0)
                {
                    ht["msg"] = "收货地址无效";
                    return new Result_Model((int)ht["status"], (string)ht["msg"], ht);
                }

                if (_model.FPayID <= 0)
                {
                    ht["msg"] = "请选择支付方式";
                    return new Result_Model((int)ht["status"], (string)ht["msg"], ht);
                }

                //if (_model.FShippingID <= 0)
                //{
                //    ht["msg"] = "请选择配送方式";
                //    return JsonHelper.SerializeObject(ht);
                //}

                if (_model.FImportsFlag < 0)
                {
                    ht["msg"] = "商品型定义无效";
                    return new Result_Model((int)ht["status"], (string)ht["msg"], ht);
                }

                long li_KInterID = 0;
                bool v = ShopCartBill.SaveOrder(_model, li_FWeixinID, li_CorpID, goodsSeckillId, ref li_KInterID);
                if (v)
                {
                    //ShopCartBill.LoadWxPayPara(li_CorpID.ToString());
                    DataTable dtPay = ShopCartBill.Get_Mall_Payment(li_KInterID);
                    decimal li_FPayRealAmount = dtPay.Rows.Count > 0 ? Convert.ToDecimal(dtPay.Rows[0]["FPayRealAmount"].ToString()) : 0;//实收金额                
                    ht["status"] = 1;
                    ht["J_KInterID"] = li_KInterID;
                    ht["msg"] = "恭喜您，保存订单成功！";
                    if (_model.FPayID == 6 || li_FPayRealAmount <= 0)
                    {
                        ShopCartBill.UpdateOrderState(li_KInterID.ToString(), "6");
                        ht["href"] = "/member.html";
                    }
                    else
                    {
                        ht["href"] = string.Format("/Order/OrderPayOauth/{0}", li_KInterID);
                    }
                }

                return new Result_Model((int)ht["status"], (string)ht["msg"], ht);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.ToString());
                //Log.Debug(ex.Message, "提交订单出错：");
                return null;
            }
        }
        #endregion



        [HttpPost, HttpGet]
        [ActionName("GetCouponList")]
        public async Task<Result_Model> GetCouponList()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long m_MemberId = CommonFun.ConvertToBigInt(HttpContext.Current.Request["MemberId"]);
            int type = CommonFun.ConvertToInt(HttpContext.Current.Request["type"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            long m_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            ArrayList dt = MallService.GetCouponList(m_CorpID, m_MemberId, type);
            status = 1;
            return new Result_Model(status, "获取成功！", dt);
        }


        [HttpPost, HttpGet]
        [ActionName("GetCouponById")]
        public async Task<Result_Model> GetCouponById()
        {
            int status = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            long m_MemberId = CommonFun.ConvertToBigInt(HttpContext.Current.Request["MemberId"]);
            long id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["id"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            long m_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            ArrayList dt = MallService.GetCouponById(m_CorpID, m_MemberId, id);
            status = 1;
            return new Result_Model(status, "获取成功！", dt?[0]);
        }


        #region 获取订单数据
        /// <summary>
        /// 获取订单数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        [ActionName("GetOrderList")]
        public Result_Model GetOrderList()
        {
            Hashtable ht = new Hashtable();
            ht["status"] = 0;
            ht["msg"] = "载入订单数据失败！";
            ht["count"] = 0;
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string strJson = CommonFun.ConvertToString(HttpContext.Current.Request["strJson"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3

            if (m_WeixinId > 0)
            {
                long li_CorpID = m_CorpID;
                T_OrderList _model = JsonConvert.DeserializeObject<T_OrderList>(strJson);
                int m_pageSize = 10;
                DataTable dt = ShopCartBill.Get_Order_Payment(m_WeixinId, li_CorpID, _model.PageIndex, m_pageSize, _model);
                if (dt != null && dt.Rows.Count > 0)
                {
                    List<GoodsJson> orderlist = RegroupOrderList(li_CorpID, dt);
                    ht["status"] = 1;
                    ht["list"] = orderlist;
                    ht["msg"] = "数据加载完毕！";
                    ht["count"] = dt.Rows[0]["FRowCount"];
                }
                else if (dt.Rows.Count == 0 || dt == null)
                {
                    ht["status"] = 1;
                    ht["list"] = new List<GoodsJson>();
                    ht["msg"] = "数据加载完毕！";
                    ht["count"] = 0;
                }
                return new Result_Model((int)ht["status"], (string)ht["msg"], ht["list"], (int)ht["count"]);

            }
            else
            {
                ht["status"] = 0;
                ht["msg"] = "载入数据失败，请先登录！";
            }
            return new Result_Model((int)ht["status"], (string)ht["msg"], ht["list"], (int)ht["count"]);
        }
        #endregion


        #region 取消订单
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public Result_Model CancelOrder()
        {
            Hashtable ht = new Hashtable();
            ht["status"] = 0;
            ht["msg"] = "取消订单失败";
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            //string strJson = CommonFun.ConvertToString(HttpContext.Current.Request["strJson"]);
            //string json = CommonFun.ConvertToString(HttpContext.Current.Request["json"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            Int64 id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["id"]);

            if (m_WeixinId > 0)
            {
                if (!ShopCartBill.IsCanCancelOrder(id))
                {
                    if (ShopCartBill.CancelOrder(id))
                    {
                        T_OrderList _mode = new T_OrderList
                        {
                            StateID = 0,
                            FBillType = -1,
                            GoodsID = -1
                        };
                        DataTable dt = ShopCartBill.Get_Order_Payment(m_WeixinId, m_CorpID, 0, 10,
                            _mode);
                        ht["status"] = 1;
                        ht["msg"] = "取消订单成功";
                        ht["count"] = dt.Rows.Count > 0 ? dt.Rows.Count : 0;
                    }
                }
                else
                {
                    ht["msg"] = "订单已审核,不能取消!";
                }
            }
            else
            {
                ht["msg"] = "没有权限取消，请先登录！";
            }
            return new Result_Model((int)ht["status"], (string)ht["msg"], ht["count"], (int)ht["count"]);
        }
        #endregion

        /// <summary>
        /// 订单详情
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        [ActionName("OrderDetail")]
        public Result_Model OrderDetail()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            int status = 0;
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            if (m_WeixinId == 0)
            {
                return new Result_Model(status, "请先登录~");
            }

            Int64 orderId = CommonFun.ConvertToBigInt(HttpContext.Current.Request["orderId"]);
            DataTable dtOrder = ShopCartBill.GetOrderDetail(m_CorpID, orderId.ToString());
            List<GoodsJson> dtGoods = new List<GoodsJson>();
            if (dtOrder != null && dtOrder.Rows.Count > 0)
            {
                foreach (DataRow dra in ShopCartBill.Get_Mall_Detail(Convert.ToInt64(dtOrder.Rows[0]["KInterID"])).Rows)
                {
                    GoodsJson goods = new GoodsJson
                    {
                        id = dra["mFGoodID"].ToString()
                    };

                    if (string.IsNullOrEmpty(dra["FThumbImage"].ToString()))
                    {
                        //goods.img = "/images/NoImage.jpg";
                    }
                    else
                    {
                        goods.img = /*ConfigurationManager.AppSettings["ImgSaveUrl"] +*/ dra["FThumbImage"].ToString();
                    }

                    goods.title = dra["FName"].ToString();
                    goods.price = CommonFun.ConvertToDecimal(dra["mFActualPrice"]).ToString();
                    goods.qty = CommonFun.ConvertToBigInt(dra["mFQty"]).ToString();
                    goods.total = CommonFun.ConvertToBigInt(dra["mFRealAmount"]).ToString();
                    dtGoods.Add(goods);
                }
            }
            status = 1;
            return new Result_Model(status, "获取成功！", new Dictionary<string, object>
            {
                { "Order",dtOrder},
                { "Goods",dtGoods}
            });
        }

        /// <summary>
        /// 确认收货
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, HttpGet]
        [ActionName("ConfirmReceipt")]
        public Result_Model ConfirmReceipt()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            int status = 0;
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_WeixinId = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            Int64 orderId = CommonFun.ConvertToBigInt(HttpContext.Current.Request["orderId"]);
            string msg = "收货失败!";
            bool flag = ShopCartBill.UpdateOrderState(orderId.ToString(), "9");
            if (flag)
            {
                status = 1;
                msg = "收货成功!";
            }
            return new Result_Model(status, msg);
        }


        /// <summary>
        /// 重新组合订单
        /// </summary>
        /// <param name="corpId"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<GoodsJson> RegroupOrderList(long corpId, DataTable dt)
        {
            List<GoodsJson> orderlist = new List<GoodsJson>();
            foreach (DataRow dr in dt.Rows)
            {
                GoodsJson _pay = new GoodsJson();
                _pay.data = new List<GoodsJson>();
                _pay.entryid = dr["MInterID"].ToString();
                _pay.number = dr["FBillNo"].ToString();
                _pay.payname = dr["PayName"].ToString();
                _pay.piece = dr["FPiece"].ToString();
                _pay.payid = dr["FPayID"].ToString();
                _pay.paytype = dr["FPayType"].ToString();
                _pay.url = string.Format("/Order/OrderPayOauth/{0}", dr["MInterID"]);
                _pay.total = CommonFun.ConvertToDecimal(dr["FPayRealAmount"]).ToString();
                _pay.StateId = dr["FStateID"].ToString();
                _pay.StateName = dr["FStateName"].ToString();
                _pay.rowId = dr["FRowId"].ToString();
                _pay.RowCount = dr["FRowCount"].ToString();
                decimal totalPrice = 0;
                foreach (DataRow dra in ShopCartBill.Get_Mall_Detail(Convert.ToInt64(dr["MInterID"].ToString())).Rows)
                {
                    GoodsJson _order = new GoodsJson();
                    _order.href = string.Format("/OrderDetail/{0}/{1}.html", corpId, dr["MInterID"]);
                    if (string.IsNullOrEmpty(dra["FThumbImage"].ToString()))
                    {
                        //_order.img = "/images/NoImage.jpg";
                    }
                    else
                    {
                        _order.img = ConfigurationManager.AppSettings["ImgSaveUrl"] + dra["FThumbImage"];
                    }

                    _order.title = dra["FName"].ToString();
                    _order.price = CommonFun.ConvertToDecimal(dra["mFActualPrice"].ToString()).ToString();
                    _order.qty = CommonFun.ConvertToDecimal(dra["mFQty"].ToString()).ToString();
                    _order.total = CommonFun.ConvertToDecimal(dra["mFRealAmount"].ToString()).ToString();
                    totalPrice += CommonFun.ConvertToDecimal(dra["mFQty"]);

                    _pay.data.Add(_order);
                }
                _pay.qty = totalPrice.ToString();
                orderlist.Add(_pay);
            }
            return orderlist;
        }

        public string SxfPay_Notify()
        {
            try
            {
                //System.IO.Stream s = this.Request.InputStream;
                Stream xmlStream = System.Web.HttpContext.Current.Request.InputStream;
                int count = 0;
                byte[] buffer = new byte[1024];
                StringBuilder builder = new StringBuilder();
                while ((count = xmlStream.Read(buffer, 0, 1024)) > 0)
                {
                    builder.Append(Encoding.UTF8.GetString(buffer, 0, count));
                }
                xmlStream.Flush();
                xmlStream.Close();
                xmlStream.Dispose();
                SystemInfo.writeServiceLog("SxfPayNotify", builder.ToString(), "1", "1A", DBHelper.ConnMaster);

                string json = builder.ToString();
                SystemInfo.writeServiceLog("SxfPayNotify", json, "1", "1A", DBHelper.ConnMaster);

                Hashtable retHt = Newtonsoft.Json.JsonConvert.DeserializeObject<Hashtable>(json);
                if (retHt != null && CommonFun.ConvertToString(retHt["code"]) == "0000")
                {
                    Hashtable respData = Newtonsoft.Json.JsonConvert.DeserializeObject<Hashtable>(CommonFun.ConvertToString(retHt["respData"]));
                    if (respData != null && CommonFun.ConvertToString(respData["bizCode"]) == "0000")
                    {
                        SystemInfo.writeServiceLog("SxfPayNotify", "OK", "1", "2B", DBHelper.ConnMaster);
                        //退款申请成功写数据库
                        //Data.AppCenterDal.UpdateSetMallDataRefund(CommonFun.ConvertToString(respData["mno"]), CommonFun.ConvertToString(respData["ordNo"]),
                        //    HttpContext.Server.MapPath("../log"));
                    }
                }
            }
            catch
            {

            }
            return "{\"code\":\"success\",\"msg\":\"成功\"}";
        }

        /// <summary>
        /// 随行付支付回调
        /// </summary>
        [HttpPost]
        [HttpGet]
        [ActionName("SxfPayNotify")]
        public NotifyResultModel SxfPayNotify()
        {
            try
            {
                Stream xmlStream = System.Web.HttpContext.Current.Request.InputStream;
                StreamReader rd = new StreamReader(xmlStream);
                string jsonStr = rd.ReadToEnd();
                SystemInfo.writeServiceLog("SxfPayNotify", jsonStr, "1", "1A", DBHelper.ConnMaster);
                NotifyModel notifyModel = (NotifyModel)JsonConvert.DeserializeObject(jsonStr, typeof(NotifyModel));
                // y_pay_order 和 mall_master 设置为已经支付
                string m_sql = "select * from y_Mall_PayOrder where FBillNo=@BillNo";
                List<SqlParameter> sqlParams = new List<SqlParameter>() {
                new SqlParameter("@BillNo",notifyModel.ordNo)
                };
                ArrayList arrayList = DBHelper.Select(m_sql, sqlParams.ToArray(), DBHelper.ConnMaster);
                if (arrayList.Count is 0)
                {
                    SystemInfo.writeServiceLog("SxfPayNotify", "jsonStr", "arrayList.Count is 0", "1B", DBHelper.ConnMaster);

                    return null;
                }
                else
                {
                    Hashtable dt = arrayList[0] as Hashtable;
                    string m_sql_update_ypay = "UPDATE y_Mall_PayOrder  Set  FPayState=1,FPayTime=@FPayTime where FBillNo = @BillNo ";
                    List<SqlParameter> sqlParams2 = new List<SqlParameter>() {
                         new SqlParameter("@BillNo",notifyModel.ordNo),
                         new SqlParameter("@FPayTime",notifyModel.payTime)
                        };

                    DBHelper.ExeSQL(m_sql_update_ypay, sqlParams2.ToArray(), DBHelper.ConnMaster);
                    SystemInfo.writeServiceLog("SxfPayNotify", "jsonStr", "m_sql_update_ypay", "1C", DBHelper.ConnMaster);

                    long MInterID = CommonFun.ConvertToBigInt(dt["MInterID"]);
                    if (MInterID > 0)
                    {
                        string m_sql_update_mall_master = "UPDATE y_Mall_Master  Set  FPayFlag=1,FStateID=1,FPayType=1 where KInterID = @KInterID ";
                        List<SqlParameter> sqlParams3 = new List<SqlParameter>() {
                         new SqlParameter("@KInterID",MInterID)
                        };
                        DBHelper.ExeSQL(m_sql_update_mall_master, sqlParams3.ToArray(), DBHelper.ConnMaster);
                        SystemInfo.writeServiceLog("SxfPayNotify", "jsonStr", "update_mall_master", "1D", DBHelper.ConnMaster);

                        return new NotifyResultModel();
                    }
                    SystemInfo.writeServiceLog("SxfPayNotify", "nothing", "update_mall_master", "1E", DBHelper.ConnMaster);

                    return null;

                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }



        [HttpPost, HttpGet]
        [ActionName("GenQRCode")]
        public async Task<Result_Model> GenQRCode(Hashtable dic)
        {
            string m_CorpID = dic["CorpID"] as string;
            ArrayList dt = DBHelper.Select(@"select FBusinessKey from  t_Corpration where FItemID=@CorpID", new List<SqlParameter>() { new SqlParameter("@CorpID", m_CorpID) }.ToArray(), DBHelper.ConnMaster);
            if (dt.Count > 0)
            {
                Hashtable ht = dt[0] as Hashtable;
                string m_FBusinessKey = ht["FBusinessKey"] as string;
                return await MallService.GetQrCode(m_FBusinessKey);
            }
            else
            {
                return new Result_Model(0, "获取失败！", 0);
            }
        }

        [HttpPost, HttpGet]
        [ActionName("GetStoreInfoByScene")]
        public Result_Model GetStoreInfoByScene()
        {
            int status = 0;
            string m_scene = CommonFun.ConvertToString(HttpContext.Current.Request["scene"]);
            ArrayList dt = DBHelper.Select(@"select FItemID as CorpId,FStockID,FParentID,FName as StoreName,FAdvUrl       from  t_Corpration where FBusinessKey=@scene", new List<SqlParameter>() { new SqlParameter("@scene", m_scene) }.ToArray(), DBHelper.ConnMaster);
            if (dt.Count > 0)
            {
                Hashtable ht = dt[0] as Hashtable;
                status = 1;
                return new Result_Model(status, "获取成功！", ht);
            }
            else
            {
                return new Result_Model(status, "获取失败！", 0);
            }
        }

        [HttpPost, HttpGet]
        [ActionName("BindStore")]
        public Result_Model BindStore()
        {

            string message;
            int status = 0;
            int CorpId = CommonFun.ConvertToInt(HttpContext.Current.Request["CorpId"]);
            string appId = CommonFun.ConvertToString(HttpContext.Current.Request["appId"]);
            string code = CommonFun.ConvertToString(HttpContext.Current.Request["code"]);


            var secret = MallCommon.GetAppSecret(appId);
            if (string.IsNullOrEmpty(secret))
            {
                message = "secret不能为空";
                return new Result_Model(status, message);
            }
            //获取openId
            JObject jo = WxService.GetOpenIdByCode(code, appId, secret);
            var unionId = CommonFun.GetJObjectValue(jo, "unionid");
            string openId = CommonFun.GetJObjectValue(jo, "openid");
            if (string.IsNullOrWhiteSpace(openId))
            {
                message = "openId获取失败";
                return new Result_Model(status, message);

            }


            ArrayList arrayList = DBHelper.Select(@"select  * from  t_UserWeixin where FCorpId =@CorpId  and FOpenID=@FOpenID", new List<SqlParameter>() {
                new SqlParameter("@CorpId", CorpId), new SqlParameter("@FOpenID", openId) }
         .ToArray(), DBHelper.ConnMaster);


            if (arrayList.Count == 0)
            {
                string sql = @"insert into t_UserWeixin(FOpenID,FCreateTime,FNiName,FCorpID) values(@FOpenID,@FCreateTime,0,@FCorpID)";
                //@FUnionID,@FNiName,@FAvatarUrl,
                SqlParameterList par_list = new SqlParameterList();
                par_list.Add("@FOpenID", SqlDbType.VarChar, openId);
                par_list.Add("@FCorpID", SqlDbType.VarChar, CorpId);
                par_list.Add("@FCreateTime", SqlDbType.DateTime2, DateTime.Now);
                //par_list.Add("@FUnionID", SqlDbType.VarChar, unionId);
                par_list.Add("@FNiName", SqlDbType.VarChar, "李");
                //par_list.Add("@FAvatarUrl", SqlDbType.VarChar, openId);
                string msg = DBHelper.ExeSQL(sql, par_list.Get_List(), DBHelper.ConnMaster);
            }

            status = 1;
            message = "绑定成功！";
            return new Result_Model(status, message, "dt");

        }



        /// <summary>
        /// 1.18、生成订单
        /// </summary>
        [HttpPost]
        [ActionName("DelShoppingCart")]
        public Result_Model DelShoppingCart()
        {
            try
            {
                string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
                Stream xmlStream = System.Web.HttpContext.Current.Request.InputStream;
                StreamReader rd = new StreamReader(xmlStream);
                string json = rd.ReadToEnd();
                ArrayList detailed = (ArrayList)PluSoft.Utils.JSON.Decode(json);
                string msg = "删除失败";

                int mResult = MallCommon.DelShoppingCart(shop_appid, detailed, ref msg);
                return new Result_Model(mResult, msg, null);
            }
            catch (Exception ex)
            {
                return new Result_Model(0, ex.Message, null);
            }
        }

        /// <summary>
        /// 1.12、商品分类列表
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("DelCart")]
        public Result_Model DelCart()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            int m_CorpID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID); // 1
            int m_UserID = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID); // 3
            long m_WeixinID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.WeixinID); // 3
            if (m_CorpID == 0 || m_WeixinID == 0)
            {
                return new Result_Model(0, "缺少参数");

            }
            bool res = MallCommon.DelCart(m_WeixinID, m_CorpID);
            return new Result_Model(res ? 1 : 0, res ? "清空成功" : "清空失败", res);
        }



        /// <summary>
        /// 1.12、商品分类列表
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetGoodClassList")]
        public Result_Model GetGoodClassList()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            ArrayList data = MallCommon.GetGoodClassList(shop_appid);
            return new Result_Model(1, "获取成功", data);
        }


        /// <summary>
        /// 1.12、生成授权链接
        /// </summary>
        [HttpPost, HttpGet]
        public Result_Model GenAuthUrl()
        {
            CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);

            int type = CommonFun.ConvertToInt(HttpContext.Current.Request["type"]);
            string authURL = ThirdPartyService.GenAuthorUrl(type);
            return new Result_Model(1, "获取成功", authURL);
        }



        /// <summary>
        /// 1.64、获取微信支付信息
        /// </summary>
        [HttpPost]
        [ActionName("SetRenewalsPay")]
        public Result_Model SetRenewalsPay()
        {
            string msg = "";
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            /*       Stream xmlStream = System.Web.HttpContext.Current.Request.InputStream;
                   StreamReader rd = new StreamReader(xmlStream);
                   string json = rd.ReadToEnd();*/

            //Hashtable row = JsonConvert.DeserializeObject<Hashtable>(json);
            string openid = CommonFun.ConvertToString(HttpContext.Current.Request["openid"]);
            //string openid = CommonFun.ConvertToString(row["openid"]);
            //decimal total_fee = CommonFun.ConvertToDecimal(row["total_fee"]);
            decimal total_fee = CommonFun.ConvertToDecimal(HttpContext.Current.Request["total_fee"]);
            int years = CommonFun.ConvertToInt(HttpContext.Current.Request["years"]);

            //int years = CommonFun.ConvertToInt(row["years"]);
            if (years == 0) years = 1;
            ArrayList rows = JsonConvert.DeserializeObject<ArrayList>(CommonFun.ConvertToString(HttpContext.Current.Request["detaileds"]));
            //ArrayList rows = row["detaileds"] as ArrayList;

            //total_fee = CommonFun.ConvertToDecimal(0.01);

            int shop_id = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            int user_id = (int)SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);

            int type = 0;//0是充值续费，2是短信充值
            Dictionary<string, object> dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(rows[0]));

            //生成支付订单
            string out_trade_no = MallCommon.SaveRenewalsOrder(shop_id, user_id, total_fee, years, ref msg, type, shop_appid, dic);
            if (out_trade_no == "") return new Result_Model(2, !string.IsNullOrEmpty(msg) ? msg : "生成订单失败");

            //开始调用支付
            Dictionary<string, object> result = new Dictionary<string, object>();

            int m_Paytype = 3;  // 3 原生  4 半屏
            bool ret = false;

            // 查询支付方式
            ArrayList list = DBHelper.Select("select  FProvider from t_ShouKuanElfin where FCorpID=@shopID", new List<SqlParameter>(){
                new SqlParameter("@shopID", shop_id)
            }.ToArray(), DBHelper.ConnMaster);

            if (list.Count is 0)
            {
                return new Result_Model(2, "未查到商户配置信息", result);
            }
            Hashtable skht = list[0] as Hashtable;
            int m_FProvider = CommonFun.ConvertToInt(skht["FProvider"]);
            if (m_FProvider == 0)
            {
                m_Paytype = 3;
            }
            else
            {
                m_Paytype = m_FProvider;
            }
            // 原生支付
            if (m_FProvider == 3)
            {
                ret = SxfPayCommon.GetCreateOrderPay(shop_id, openid, total_fee, out_trade_no, ref msg, out result);
            }
            // 半屏支付
            else if (m_FProvider == 4)
            {
                ret = SxfPayCommon.GetCreateOrderPayAppletScanPre(shop_id, total_fee, out_trade_no, ref msg, out result);
            }
            // 插件支付
            /*
                 else if (m_Paytype == 5)
                 {

                 }
            */


            if (!ret) return new Result_Model(2, !string.IsNullOrEmpty(msg) ? msg : "获取失败");

            //查询支付结果
            MallCommon.SetRepeatQueryPay(rows, out_trade_no, shop_id, years, type, shop_appid);
            return new Result_Model(1, "获取成功", result);
        }




    }
}